#! /bin/sh

#
# Create an ELF file with partial pages and verify that missing parts
# are filled with zeroes.
#

mkdir -p out || exit 99

name=$( basename "$0" )
datafile="out/${name}.data"
dumpfile="out/${name}.dump"
resultfile="out/${name}.result"
expectfile="$srcdir/partial.expect"

cat >"$datafile" <<EOF
@phdr type=LOAD offset=0x1000 vaddr=0x200 paddr=0x200 memsz=0x400
55*0x200
@phdr type=LOAD vaddr=0x800 paddr=0x800 memsz=0x400
@phdr type=LOAD vaddr=0xc00 paddr=0xc00 memsz=0x200
aa*0x200
EOF

./mkelf "$dumpfile" <<EOF
ei_class = 2
ei_data = 1
e_machine = 62
e_phoff = 64

DATA = $datafile
EOF
rc=$?
if [ $rc -ne 0 ]; then
    echo "Cannot create ELF file" >&2
    exit $rc
fi
echo "Created ELF dump: $dumpfile"

./dumpdata "$dumpfile" 0 4096 >"$resultfile"
rc=$?
if [ $rc -ne 0 ]; then
    echo "Cannot dump ELF data" >&2
    exit $rc
fi

if ! diff "$expectfile" "$resultfile"; then
    echo "Results do not match" >&2
    exit 1
fi
